package com.xgk.boot.module.core.controller.admin.product;


import com.xgk.boot.framework.common.core.ServerOption;
import com.xgk.boot.framework.common.pojo.CommonResult;
import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.common.util.object.BeanUtils;
import com.xgk.boot.framework.security.core.util.SecurityFrameworkUtils;
import com.xgk.boot.module.core.controller.admin.product.vo.product.ProductInfoVO;
import com.xgk.boot.module.core.controller.admin.product.vo.product.ProductPageReqVO;
import com.xgk.boot.module.core.controller.admin.product.vo.product.ProductPageRespVO;
import com.xgk.boot.module.core.controller.admin.product.vo.product.ProductSaveReqVO;
import com.xgk.boot.module.core.dal.entity.product.ProductDO;
import com.xgk.boot.module.core.dal.iservice.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.xgk.boot.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 产品")
@RestController
@RequestMapping("/admin/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @PostMapping("/create")
    @Operation(summary = "创建产品")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<Long> createProduct(@Valid @RequestBody ProductSaveReqVO createReqVO, HttpServletRequest request) throws IOException {
        System.out.println("Request Body: " + request.getReader().lines().collect(Collectors.joining()));

        return success(productService.createProduct(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "修改产品")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<Boolean> updateProduct(@Valid @RequestBody ProductSaveReqVO updateReqVO) {
        productService.updateProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除产品")
    @Parameter(name = "id", description = "扣费规则id", required = true, example = "1934923143484133377")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<Boolean> deleteProduct(@PathVariable("id") Long id) {
        boolean b = productService.deleteProduct(id);
        return success(b);
    }

    @PostMapping("/active")
    @Operation(summary = "启用产品")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<Boolean> activeProduct(@RequestParam Long prId, @RequestParam Integer prStatus) {
        productService.activeProduct(prId, prStatus);
        return success(true);
    }

    @PostMapping("/list")
    @Operation(summary = "获取产品列表")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<List<ProductInfoVO>> listProduct(@RequestBody ProductPageReqVO queryVo) {
        List<ProductDO> productDOS = productService.listProduct(queryVo);
        return success(BeanUtils.toBean(productDOS, ProductInfoVO.class));
    }


    @GetMapping("/info/{id}")
    @Operation(summary = "获取商品详细配置")
    @Parameter(name = "id", description = "扣费规则id", required = true, example = "1934923143484133377")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<ProductInfoVO> getProductInfoById(@PathVariable("id") Long id) {
        ProductInfoVO productInfoById = productService.getProductInfoCacheById(id);
        return success(productInfoById);
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品分页")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<PageResult<ProductPageRespVO>> getProductPage(ProductPageReqVO pageReqVO) {
        PageResult<ProductPageRespVO> pageResult = productService.getProductPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping(value = "/selectProduct")
    public CommonResult<List<ServerOption>> getApiSelectListNotInSp() {
        String username = SecurityFrameworkUtils.getLoginUser().getUsername();
        List<Map> list = productService.getProductByUserName(username);
        List<ServerOption> maps =
                list.stream().map(d -> new ServerOption<>(d.get("p_code"), d.get("p_name") + "[" + d.get("p_code") + "]")).collect(Collectors.toList());
        return success(maps);
    }

 

}
